package zombiedefense.effects;

import java.awt.Color;
import java.awt.Graphics2D;
import zombiedefense.map.TileMap;
import zombiedefense.util.MathEx;
import zombiedefense.util.Vector2D;
import static zombiedefense.util.Vector2D.scratch;
import static zombiedefense.util.Vector2D.scratch2;

public class Tracer extends Effect{
	
	private Vector2D end = new Vector2D();   //holds coords of other point in the effect (besides that of effect itself)
        private boolean drawLine;
        
        //distance of effect from player
        private static final double lineMinDist = 1.0;
        private static final double lineMaxDist = 3.0;
        private double scale;
        
        //length of effect
        private static final double lineLength = 1.0;
        
        //tracer color
        private static final Color c = new Color(1.0f,1.0f,1.0f,.8f);
	
	public void setFields(double x,double y,double barrelx,double barrely,long effectTime){
	    super.setFields(x,y,effectTime);
              
            end.setTo(x,y);    //vector to end of effect
            scratch.setTo(barrelx,barrely);  //vector to barrel of gun
            
            end.subtract(scratch);   //find vector between start and end
            
            scale = MathEx.getRandomDouble(lineMinDist,lineMaxDist);  //recalc scale
            
            //see if there is room to draw the tracer
            double l = end.length();
            if(l >= scale + lineLength){
                
                //normalize end
                end.divide(l);
                
                //set to desired length
                scratch2.setTo(end); //use scratch for efficiency
                scratch2.scale(scale);

                //save these coords as first point
                this.x = scratch2.x + barrelx;
                this.y = scratch2.y + barrely;

                //scale to second distance
                end.scale(scale + lineLength);

                //save as second point
                end.add(scratch);
                
                drawLine = true;
            }else{
                //do not draw tracer if distance is too short
                drawLine = false;
            }   
        }
	
	public void draw(Graphics2D g,int drawx,int drawy,TileMap map){
            if(!drawLine)return;
            
            //white line
	    g.setColor(c);
            
	    //draw tracer
	    g.drawLine(drawx,drawy,map.getScreenX(end.x),map.getScreenY(end.y));
	}
    }